<include file="Index/header" />
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>{$title}</strong></div>
  <div class="body-content">
    <form class="form-x"  enctype="multipart/form-data" name="user">
      <div class="form-group">
        <div class="label">
          <label>用户名：</label>
        </div>
        <div class="field">
            <input type="hidden" name="userId" value="{$userId}">
          <input type="text" class="input w50" value="{$username}" name="username"  placeholder="请输入用户名"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="username" style="color:#F00;"></span>
          <div class="tips"></div>
        </div>
      </div>
        <div class="form-group">
            <div class="label">
                <label>密码：</label>
            </div>
            <div class="field">
                <input type="password" class="input w50" value="{$password}" name="password"  placeholder="请输入密码" readonly="readonly" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="password" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>确认密码：</label>
            </div>
            <div class="field">
                <input type="password" class="input w50" value="{$password}" name="repassword"  placeholder="请输入密码" readonly="readonly" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="repassword" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>昵称：</label>
            </div>
            <div class="field">
                <input type="text" class="input w50" value="{$eName}" name="eName"  placeholder="请输入昵称"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="eName" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>分配角色：</label>
            </div>
            <div class="field">
                <div>
                <foreach name="role" item="vo">
                    <if condition="$key % 5 eq 0">
                        <br /><br />
                    </if>
                    <input type="checkbox" name="role_id[]" value="{$vo['id']}" <if condition="in_array($vo['id'],$have_role) eq true">checked=checked</if>/>{$vo['role_name']}&nbsp;&nbsp;
                </foreach></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="role_id" style="color:#F00; margin-left: 350px;"></span>
                <div class="tips"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="label">
                <label>联系方式：</label>
            </div>
            <div class="field">
                <input type="text" class="input w50" value="{$tel}" name="tel"   placeholder="请输入联系方式"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="tel" style="color:#F00;"></span>
                <div class="tips"></div>
            </div>
        </div>
      <div class="clear"></div>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <div class="button bg-main icon-check-square-o tijiao" style="cursor:pointer;"> 提交</div>
        </div>
      </div>
    </form>
  </div>
</div>

</body></html>
<script>
    //验证手机号的唯一性
    function checkTel(tel,obj){
        var id=$("input[name='userId']").val();
        $.ajax({
            url:'/Admin/Admin/checkTelUnique',
            type:'post',
            data:{tel:tel,type:"1",id:id},
            dataType:'json',
            success:function(re){
                obj.html(re.tips);
            }
        });
    }
   //验证用户唯一性
    function checkUser(username,obj){
        var id=$("input[name='userId']").val();
        $.ajax({
            url:'/Admin/Admin/checkUserUnique',
            type:'post',
            data:{username:username,type:'1',id:id},
            dataType:'json',
            success:function(re){
                obj.html(re.tips);
            }
        });
    }
    //验证用户名
    var attr_name=obj_tips='';
    $('input[name="username"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('用户名不能为空！');
        }else{
            checkUser($(this).val(),obj_tips);
        }
    });
     //验证昵称
    $('input[name="eName"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('昵称不能为空！');
        }else{
            obj_tips.html('');
        }
    });

   //验证联系方式
    var regexp_mobile=/^1[3-9]\d{9}$/;
    var regexp_tel=/^0([12]\d{9}|[3-9]\d{9,10})$/;
    $('input[name="tel"]').blur(function(){
        attr_name=$(this).prop('name');
        obj_tips=$('#'+attr_name);
        if($(this).val()==''){
            obj_tips.html('联系方式不能为空！');
        }else if(!regexp_mobile.test($(this).val()) && !regexp_tel.test($(this).val())){
            obj_tips.html('联系方式不合法！');
        }else{
            checkTel($(this).val(),obj_tips);
        }
    });

    var username=eName=tel=role_id='';
    function checkForm(){
        username=$('input[name="username"]').val();
        if(username==''){
            $('#username').html('用户名不能为空！');
            return false;
        }
        eName=$('input[name="eName"]').val();
        if(eName==""){
            $("#eName").html("昵称不能为空！");
            return false;
        }else{
            $("#eName").html("");
        }
        if($("input:checkbox:checked").length<=0){
            $('#role_id').html('角色至少选择一项！');
            return false;
        }else{
            $('#role_id').html('');
        }
        tel=$('input[name="tel"]').val();
        if(tel==''){
            $('#tel').html('联系方式不能为空！');
            return false;
        }else if(!regexp_mobile.test(tel) && !regexp_tel.test(tel)){
            $('#tel').html('联系方式不合法！');
            return false;
        }
    }

    function postData(){
        var id=$("input[name='userId']").val();
        var password=$("input[name='password']").val();
        var repassword=$("input[name='repassword']").val();
        var data={username:username,eName:eName,tel:tel,id:id,userId:id,password:password,repassword:repassword};
        $.ajax({
            url:'/Admin/Admin/ajaxCheckData/scene/editAdmin',
            type:'post',
            data:data,
            dataType:'json',
            success:function(re){
                if(re.error==1){
                    $.each(re.errInfo,function(key,val){
                        $('#'+key).html(val);
                    })
                    window.location.href='#mask';
                }else{
                    $('form[name="user"]').prop('action','editAdmin').prop('method','post');
                    $('form[name="user"]').submit();
                }
            }
        });
    }

    $('.tijiao').click(function(){
        var flag;
        flag=checkForm();//提交前的验证
        if(flag===false){
            window.location.href='#mask';
        }else{
            postData();//发送数据进行验证
        }
    });
</script>